import { createRouter, createWebHashHistory } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// ==================== 基础路由配置 ====================

// 认证相关路由
const authRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true,
      title: "登录"
    },
  },
  {
    path: "/register",
    component: () => import("@/views/register/index.vue"),
    meta: {
      hidden: true,
      title: "注册"
    },
  },
];

// 主页路由
const dashboardRoutes = [
  {
    path: "/",
    name: "Home",
    component: Layout,
    redirect: "/dashboard",
    meta: { 
      title: "首页", 
      icon: "homepage" 
    },
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/home/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
    ],
  },
];

// 工位管理路由
const workstationRoutes = [
  {
    path: "/workstation",
    name: "Workstation",
    component: Layout,
    meta: { 
      title: "工位管理", 
      icon: "monitor" 
    },
    children: [
      {
        path: "system-overview",
        component: () => import("@/views/workstation/system-overview.vue"),
        name: "SystemOverview",
        meta: {
          title: "系统总体图",
          icon: "document",
          keepAlive: true,
        },
      },
      {
        path: "alarm-control",
        component: () => import("@/views/workstation/alarm-control.vue"),
        name: "AlarmControl",
        meta: {
          title: "报警控制终端",
          icon: "warning",
          keepAlive: true,
        },
      },
    ],
  },
];

// 用户相关路由
const userRoutes = [
  {
    path: "/personal",
    name: "Personal",
    component: Layout,
    redirect: "/personal/homepage",
    meta: {
      hidden: true,
      title: "个人中心"
    },
    children: [
      {
        path: "homepage",
        component: () => import("@/views/personalHomepage/index.vue"),
        name: "PersonalHomepage",
        meta: {
          title: "个人主页",
          icon: "wode",
          keepAlive: true,
        },
      },
    ],
  },
  {
    path: "/settings",
    name: "Settings",
    component: Layout,
    redirect: "/settings/index",
    meta: { 
      title: "设置", 
      icon: "setting", 
      hidden: true 
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/settings/index.vue"),
        name: "SettingsIndex",
        meta: {
          title: "系统设置",
          icon: "setting",
          keepAlive: true,
        },
      },
    ],
  },
];

// 详情页路由
const detailRoutes = [
  {
    path: "/detail/:index",
    component: Layout,
    redirect: "/detail/:index",
    meta: {
      title: "详情",
      hidden: true,
      icon: "detail",
    },
    children: [
      {
        path: "",
        name: "Detail",
        component: () => import("@/views/detail/index.vue"),
        meta: { 
          title: "详情页" 
        },
      },
    ],
  },
];

// ==================== 功能性菜单路由（拆分为独立菜单） ====================

// 表单相关路由
const formRoutes = [
  {
    path: "/form",
    name: "Form",
    component: Layout,
    redirect: "/form/index",
    meta: {
      title: "表单",
      icon: "form",
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/features/form/index.vue"),
        name: "FormIndex",
        meta: {
          title: "表单",
          icon: "form",
          keepAlive: true,
        },
      },
    ],
  },
];

// 表格路由
const tableRoutes = [
  {
    path: "/table",
    name: "Table",
    component: Layout,
    redirect: "/table/index",
    meta: {
      title: "表格示例",
      icon: "biaoge",
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/features/table/index.vue"),
        name: "TableIndex",
        meta: {
          title: "表格示例",
          icon: "biaoge",
          keepAlive: true,
        },
      },
    ],
  },
];

// 文件处理路由
const fileRoutes = [
  {
    path: "/file",
    name: "File",
    component: Layout,
    redirect: "/file/upload",
    meta: {
      title: "文件处理",
      icon: "folder",
      alwaysShow: true,
    },
    children: [
      {
        path: "upload",
        component: () => import("@/views/features/upload/index.vue"),
        name: "Upload",
        meta: {
          title: "文件上传",
          icon: "upload",
          keepAlive: true,
        },
      },
      {
        path: "pdf",
        component: () => import("@/views/features/pdf/index.vue"),
        name: "Pdf",
        meta: {
          title: "PDF预览及打印",
          icon: "pdf",
          keepAlive: true,
        },
      },
      {
        path: "excel",
        component: () => import("@/views/features/excel/index.vue"),
        name: "Excel",
        meta: {
          title: "Excel表格导入导出",
          icon: "excel",
          keepAlive: true,
        },
      },
      {
        path: "word",
        component: () => import("@/views/features/word/index.vue"),
        name: "Word",
        meta: {
          title: "Word文档预览",
          icon: "word",
          keepAlive: true,
        },
      },
    ],
  },
];

// 电子签名路由
const esignRoutes = [
  {
    path: "/esign",
    name: "Esign",
    component: Layout,
    redirect: "/esign/index",
    meta: {
      title: "电子签名",
      icon: "qianming",
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/features/esign/index.vue"),
        name: "EsignIndex",
        meta: {
          title: "电子签名",
          icon: "qianming",
          keepAlive: true,
        },
      },
    ],
  },
];

// 审批流程路由
const approvalRoutes = [
  {
    path: "/approval",
    name: "Approval",
    component: Layout,
    redirect: "/approval/index",
    meta: {
      title: "OA审批流",
      icon: "document",
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/features/approval/index.vue"),
        name: "ApprovalIndex",
        meta: {
          title: "OA审批流概览",
          icon: "document",
          keepAlive: true,
        },
      },
      {
        path: "my-applications",
        component: () => import("@/views/features/approval/my-applications.vue"),
        name: "MyApplications",
        meta: {
          title: "我的申请",
          icon: "document",
          keepAlive: true,
        },
      },
      {
        path: "pending-tasks",
        component: () => import("@/views/features/approval/pending-tasks.vue"),
        name: "PendingTasks",
        meta: {
          title: "待办任务",
          icon: "warning",
          keepAlive: true,
        },
      },
      {
        path: "completed-tasks",
        component: () => import("@/views/features/approval/completed-tasks.vue"),
        name: "CompletedTasks",
        meta: {
          title: "已办任务",
          icon: "document",
          keepAlive: true,
        },
      },
      {
        path: "cc-to-me",
        component: () => import("@/views/features/approval/cc-to-me.vue"),
        name: "CcToMe",
        meta: {
          title: "抄送我的",
          icon: "document",
          keepAlive: true,
        },
      },
      {
        path: "process-monitoring",
        component: () => import("@/views/features/approval/process-monitoring.vue"),
        name: "ProcessMonitoring",
        meta: {
          title: "流程监控",
          icon: "monitor",
          keepAlive: true,
        },
      },
      {
        path: "statistics-reports",
        component: () => import("@/views/features/approval/statistics-reports.vue"),
        name: "StatisticsReports",
        meta: {
          title: "统计报表",
          icon: "biaoge",
          keepAlive: true,
        },
      },
    ],
  },
];

// 图表可视化路由
const chartRoutes = [
  {
    path: "/chart",
    name: "Chart",
    component: Layout,
    redirect: "/chart/echarts",
    meta: {
      title: "图表可视化",
      icon: "keshihua",
      alwaysShow: true,
    },
    children: [
      {
        path: "echarts",
        component: () => import("@/views/features/echarts/index.vue"),
        name: "Echarts",
        meta: {
          title: "ECharts图表",
          icon: "keshihua",
          keepAlive: true,
        },
      },
      {
        path: "gantt",
        component: () => import("@/views/features/gantt/index.vue"),
        name: "Gantt",
        meta: {
          title: "甘特图",
          icon: "timeline",
          keepAlive: true,
        },
      },
      {
        path: "three",
        name: "Three",
        component: () => import("@/views/features/three/index.vue"),
        meta: {
          title: "Three.js 3D",
          icon: "3d",
          keepAlive: true,
        },
      },
    ],
  },
];

// 打印模板路由
const printRoutes = [
  {
    path: "/print",
    name: "Print",
    component: Layout,
    redirect: "/print/hiprint",
    meta: {
      title: "打印模板",
      icon: "template",
      alwaysShow: true,
    },
    children: [
      {
        path: "hiprint",
        component: () => import("@/views/features/hiprint/index.vue"),
        name: "Hiprint",
        meta: {
          title: "自定义设计模板",
          icon: "template",
          keepAlive: true,
        },
      },
      {
        path: "processTicket",
        component: () => import("@/views/features/processTicket/index.vue"),
        name: "ProcessTicket",
        meta: {
          title: "发票工序票",
          icon: "ticket",
          keepAlive: true,
        },
      },
    ],
  },
];

// 流程设计路由
const processRoutes = [
  {
    path: "/process",
    name: "Process",
    component: Layout,
    redirect: "/process/index",
    meta: {
      title: "流程设计",
      icon: "process",
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/features/process/index.vue"),
        name: "ProcessIndex",
        meta: {
          title: "流程设计",
          icon: "process",
          keepAlive: true,
        },
      },
    ],
  },
];

// 多级菜单示例路由
const nestedRoutes = [
  {
    path: "/nested",
    name: "Nested",
    component: Layout,
    redirect: "/nested/level1",
    meta: {
      title: "多级菜单",
      icon: "duojixuanze",
      alwaysShow: true,
    },
    children: [
      {
        path: "level1",
        component: () => import("@/views/features/netsed/level1/index.vue"),
        name: "Level1",
        meta: {
          title: "部门管理",
          icon: "department",
          keepAlive: true,
        },
      },
      {
        path: "level2",
        component: () => import("@/views/features/netsed/level2/index.vue"),
        name: "Level2",
        meta: {
          title: "二级页面",
          icon: "level",
          keepAlive: true,
        },
      },
    ],
  },
];

// 特效展示路由
const effectRoutes = [
  {
    path: "/effects",
    name: "Effects",
    component: Layout,
    redirect: "/effects/fullpage",
    meta: {
      title: "特效展示",
      icon: "effect",
    },
    children: [
      {
        path: "fullpage",
        name: "FullPage",
        component: () => import("@/views/fullpage/index.vue"),
        meta: { 
          title: "全屏滚动特效",
          icon: "fullscreen",
          keepAlive: true,
        },
      },
    ],
  },
];

// ==================== 路由汇总 ====================

// 静态路由配置
export const constantRoutes = [
  ...authRoutes,
  ...dashboardRoutes,
  ...workstationRoutes,
  ...userRoutes,
  ...detailRoutes,
  ...formRoutes,
  ...tableRoutes,
  ...fileRoutes,
  ...esignRoutes,
  ...approvalRoutes,
  ...chartRoutes,
  ...printRoutes,
  ...processRoutes,
  ...nestedRoutes,
  ...effectRoutes,
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

export default router;
